package com.cloudlead.shipownerAction.service.impl;
import com.cloudlead.common.lang.persistence.*;
import com.cloudlead.common.worker.client.exception.RestClientException;
import com.cloudlead.commonData.service.BaseCompanyService;
import com.cloudlead.commonData.service.BaseDockService;
import com.cloudlead.commonData.service.BaseShipRouteService;
import com.cloudlead.commonData.service.BaseVoyageService;
import com.cloudlead.commonData.vo.*;
import com.cloudlead.shipownerAction.service.BaseVoyageEditSer;
import com.cloudlead.shipownerService.vo.BaseVoyageCreateVo;
import com.cloudlead.shipownerService.vo.BaseVoyageVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by Jinnoo on 2015/8/12.
 */
@Service
public class BaseVoyageEditSerImpl implements BaseVoyageEditSer {
    @Autowired//必须要的
    BaseVoyageService baseVoyageService;//spring注入
    @Autowired
    BaseShipRouteService baseShipRouteService;
    @Autowired
    BaseDockService baseDockService;
    @Autowired
    BaseCompanyService  baseCompanyService;

    //航次创建
    @Override
    public void createVoyage(BaseVoyageCreateVo vo) {
        BaseVoyage baseVoyage=new BaseVoyage();
        baseVoyage.setVessel(vo.getVessel());
        baseVoyage.setVoyage(vo.getVoyage());
        if(baseVoyageService.findOne(baseVoyage)!=null){
            throw new RestClientException("该航次已存在！");
        }
        BaseVoyage bv=new BaseVoyage();
        BeanUtils.copyProperties(vo, bv);//拷贝vo到bv
        BaseCompany bc = new BaseCompany();
        if(vo.getBaseCompany()!=null && !"".equals(vo.getBaseCompany()))
            bc.setId(vo.getBaseCompany());//将公司Id放入bc
        BaseShip bs = new BaseShip();
        if(vo.getBaseShip()!=null)
            bs.setId(vo.getBaseShip());//将船舶ID放入bs
        bv.setBaseCompany(bc);
        bv.setBaseShip(bs);
//        bv.setBaseShipRoute(bsr);
        bv.setFlag("0");//设删除位为0
//        bv.setBaseShipRoute(bsr);
        bv.setVoyage(vo.getVoyage());
        bv.setArriveExpectedTime(vo.getArriveExpectedTime());
        bv.setArriveTime(vo.getArriveTime());
        bv.setStartExpectedTime(vo.getStartExpectedTime());
        bv.setStartTime(vo.getStartTime());
        baseVoyageService.create(bv);//创建
    }
    //航次编辑
    @Override
    public void editVoyage(BaseVoyageVo vo) {
        BaseVoyage baseVoyage=new BaseVoyage();
        baseVoyage.setVessel(vo.getVessel());
        baseVoyage.setVoyage(vo.getVoyage());
        BaseVoyage baseVoyage1=baseVoyageService.findOne(baseVoyage);

        if(baseVoyage1!=null&&!baseVoyage1.getId().equals(vo.getId())){
            throw new RestClientException("该航次已存在！");
        }
        BaseVoyage bv = baseVoyageService.view(vo.getId());//通过ID查询航次对象
        BeanUtils.copyProperties(vo, bv);//将vo数据替换bv
        if(vo.getBaseCompany()!=null && !"".equals(vo.getBaseCompany())) {//替换公司
            BaseCompany bc  = new BaseCompany();
            bc.setId(vo.getBaseCompany());
            bv.setBaseCompany(bc);
        }
        if(vo.getBaseShip()!=null){//替换船舶
            BaseShip bs =new BaseShip();
            bs.setId(vo.getBaseShip());
            bv.setBaseShip(bs);
        }
        bv.setArriveExpectedTime(vo.getArriveExpectedTime());
        bv.setArriveTime(vo.getArriveTime());
        bv.setStartExpectedTime(vo.getStartExpectedTime());
        bv.setStartTime(vo.getStartTime());
        baseVoyageService.update(bv);//更新
    }

    //单值查询
    @Override
    public Object findOneVoyage(BaseVoyageVo vo) {
        BaseVoyage bv = new BaseVoyage();
        BeanUtils.copyProperties(vo, bv);//将vo拷贝到bv
        BaseVoyage bv1=baseVoyageService.findOne(bv);
        return  bv1;//单值查询
    }

    //分页查询
    @Override
    public Page<BaseVoyageVo> findAllVoyage(BaseVoyageVo bv,int size,int rows) {
        PageRequest pr =new PageRequest();
        List<Term> tr=new ArrayList<Term>();
            tr.add(new Term("flag","0"));//未删除




        //查询条件

        if(bv.getRouteId()!=null&&!"".equals(bv.getRouteId()))
            tr.add(new Term("baseShipRou.id",bv.getRouteId()));//ID
        if(bv.getId()!=null&&(!bv.getId().equals("")))
            tr.add(new Term("id",bv.getId()));//ID
        if(bv.getBaseShip()!=null) {
            BaseShip bs=new BaseShip();
            bs.setId(bv.getBaseShip());
            tr.add(new Term("baseShip", bs));//船舶
        }
        if(bv.getBaseCompany()!=null) {
            tr.add(new Term("base_company_id", bv.getBaseCompany()));//公司
        }
        if(bv.getVoyage()!=null&&(!bv.getVoyage().equals("")))
            tr.add(new Term("voyage",Operator.LK,"%"+bv.getVoyage()+"%"));
        if(bv.getVessel()!=null&&(!bv.getVessel().equals("")))
            tr.add(new Term("vessel",Operator.LK,"%"+bv.getVessel()+"%"));

        Filter fi =new Filter(new Criteria( tr));
            pr.setPage(rows);//页码
            pr.setSize(size);//每页数量
            pr.setFilter(fi);
        Page<BaseVoyage> pl=baseVoyageService.findAll(pr);
        List<BaseVoyage> lbv= pl.getContent();
        List<BaseVoyage> lbv1=new ArrayList<BaseVoyage>();
        for(Iterator<BaseVoyage> it = lbv.iterator();it.hasNext();) {//遍历
            BaseVoyage bv1= it.next();
                //航次
/*                BaseShipRoute bsr=baseShipRouteService.view(bv1.getBaseShipRoute().getId());
                if(bsr.getBaseDock()!=null)
                    bsr.setBaseDock(baseDockService.view(bsr.getBaseDock().getId()));
                if(bsr.getBaseDockI()!=null)
                    bsr.setBaseDockI(baseDockService.view(bsr.getBaseDockI().getId()));
               bv1.setBaseShipRoute(bsr);*/
                //公司
            if(bv1.getBaseCompany()!=null)
                bv1.setBaseCompany(baseCompanyService.view(bv1.getBaseCompany().getId()));
            lbv1.add(bv1);
        }
        pl.setContent(lbv1);
        Page<BaseVoyageVo> px=new  Page<BaseVoyageVo>();
        BeanUtils.copyProperties(pl,px);
        return  px;
    }
    @Override
    public void delVoyage(String id) {
        BaseVoyage bv = new BaseVoyage();
        bv =  baseVoyageService.view(id);
        bv.setFlag("1");
        baseVoyageService.update(bv);
    }
}
